package com.example.studytest.Controller.OpenData;

import javax.xml.transform.Result;
import java.util.*;

public class Utils {

    public static List<Map<String,Object>> handleData(List<Map<String, Object>> DataList){
        String[] NeedsKeys = {"id","exp_date","exp_name","invited_code","exp_session","modality","exp_type","test_session","trial_index","itemIndex","stimulus","user_response","user_response_stimulus","button_pressed","rt",
                "time_elapsed","correct","total_score"};


        Set<String> Needset = new HashSet<String>();

        Collections.addAll(Needset, NeedsKeys);
//        ArrayList KeyList = new ArrayList(Arrays.asList(NeedsKeys)) ;

        List<Map<String,Object>> ResultList = new ArrayList<Map<String,Object>>();

        for (Map<String,Object> column_data : DataList){
            Map<String,Object> new_data = new HashMap<String, Object>();
            for (String key: Needset){
                if (column_data.keySet().contains(key)){
                    new_data.put(key,column_data.get(key));
                }
                else{
                    new_data.put(key,"null");
                }
                String modality = new_data.get("exp_name").toString();
                if (modality.substring(modality.length()-3, modality.length()).equals("laa")){
                    new_data.put("modality", modality.substring(modality.length()-3, modality.length()-1));
                }
                else{
                    new_data.put("modality", modality.substring(modality.length()-2, modality.length()));
                }

            }
            ResultList.add(new_data);
        }

        return SortListByKey(ResultList);
    }

    public static List<Map<String,Object>> handleWMData(List<Map<String, Object>> DataList, Boolean IsAural){
        String[] NeedsKeys = {"id","exp_date","exp_name","invited_code","exp_session","modality","exp_type","test_session","trial_index","itemIndex","stimulus","user_response","user_response_stimulus","button_pressed","rt",
                "time_elapsed","correct","total_score"};


        Set<String> Needset = new HashSet<String>();

        Collections.addAll(Needset, NeedsKeys);
//        ArrayList KeyList = new ArrayList(Arrays.asList(NeedsKeys)) ;

        List<Map<String,Object>> ResultList = new ArrayList<Map<String,Object>>();

        for (Map<String,Object> column_data : DataList){
            Map<String,Object> new_data = new HashMap<String, Object>();
            for (String key: Needset){
                if (column_data.keySet().contains(key)){
                    new_data.put(key,column_data.get(key));
                }
                else{
                    if (key.equals("exp_name")){
                        if (IsAural){
                            new_data.put("exp_name","aural_wm");
                        }
                        else {
                            new_data.put("exp_name","written_wm");
                        }
                    }
                    else{
                        new_data.put(key,"null");
                    }

                    int S_score = Integer.parseInt((String)column_data.get("sentence_score"));
                    int L_score = Integer.parseInt((String)column_data.get("letter_score"));
                    int total_score =  S_score + L_score;

                    new_data.put("total_score", String.valueOf(total_score));

                    String modality = new_data.get("exp_name").toString();
                    new_data.put("modality", modality.substring(modality.length()-2, modality.length()));

                }
            }
            ResultList.add(new_data);
        }

        return SortListByKey(ResultList);
    }

    public static List<Map<String,Object>> SortListByKey(List<Map<String, Object>> DataList){
        String[] heads = {"id","exp_date","exp_name","invited_code","exp_session","modality","exp_type","test_session","trial_index","itemIndex","stimulus","user_response","user_response_stimulus","button_pressed","rt",
                "time_elapsed","correct","total_score"};

//        Set<String> headsSet = new HashSet<String>();
//        Collections.addAll(headsSet, heads);

        List<Map<String,Object>> ResultList = new ArrayList<Map<String,Object>>();

        for (Map<String,Object> column_data : DataList){

            Map<String,Object> item = new LinkedHashMap<>();
            for(int i = 0;i<heads.length;i++){
                String key = heads[i];
                item.put(key,column_data.get(key));
            }
//            System.out.println(item);
            ResultList.add(item);
        }


        return ResultList;

    }

}
